package com.abaaba.project2.controller;


import com.abaaba.project2.bean.Role;
import com.abaaba.project2.bean.bo.PermissionGetBo;
import com.abaaba.project2.bean.bo.PermissionPostBo;
import com.abaaba.project2.bean.bo.RoleListBo;
import com.abaaba.project2.bean.vo.BaseRespVo;
import com.abaaba.project2.bean.vo.GeneralListVo;
import com.abaaba.project2.bean.vo.PermissionsVo;
import com.abaaba.project2.bean.vo.RoleVo;
import com.abaaba.project2.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {

    @Autowired
    AdminRoleService adminRoleService;

    @RequestMapping("/options")
    public BaseRespVo getRoleOptions()
    {
        List<RoleVo> roleVos = adminRoleService.getRoleOptions();

        return BaseRespVo.ok(roleVos);
    }
    @RequestMapping("/list")
    public BaseRespVo getRoleList(RoleListBo roleListBo)
    {
        GeneralListVo<Role> roleGeneralListVo = adminRoleService.getRoleList(roleListBo);

        return BaseRespVo.ok(roleGeneralListVo);
    }

    @RequestMapping("/create")
    public BaseRespVo createRole(@RequestBody Role role)
    {
        Date date = new Date(System.currentTimeMillis());
        role.setAddTime(date);
        role.setUpdateTime(date);

        Role roleVo = adminRoleService.createRole(role);

        return BaseRespVo.ok(roleVo);
    }

    @RequestMapping("/update")
    public BaseRespVo updateRole(@RequestBody Role role)
    {
        adminRoleService.updateRole(role);

        return BaseRespVo.ok();
    }

    @RequestMapping("/delete")
    public BaseRespVo deleteRole(@RequestBody Role role)
    {

        Integer status = adminRoleService.deleteRole(role);
        if (status == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("有管理员正在使用此身份");

    }

    //permissions for get
    @RequestMapping(value = "permissions",method = RequestMethod.GET)
    public BaseRespVo getPermissions(Integer roleId)
    {
        PermissionsVo permissionsVo = adminRoleService.getPermissions(roleId);

        return BaseRespVo.ok(permissionsVo);
    }

    //permissions for post
    @RequestMapping(value = "permissions",method = RequestMethod.POST)
    public BaseRespVo postPermissions(@RequestBody PermissionPostBo permissionPostBo)
    {
        Integer status = adminRoleService.postPermissions(permissionPostBo);
        if (status == 1){
        return BaseRespVo.ok();
        }
        else
        {
            return BaseRespVo.fail("不允许修改超级管理员权限");
        }
    }


}
